﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Role</title>
    <link href="~/lib/layui/css/layui.css" rel="stylesheet" />
    <script src="~/lib/layui/layui.js"></script>
    <script src="~/js/jquery.min.js"></script>
</head>
<body>
    <table class="layui-hide" id="demo" lay-filter="test"></table>
    <script id="barDemo" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="Power">权限</a>
    </script>
    <script id="toolbarDemo" type="text/html">
        <button class="layui-btn layui-btn-sm" lay-event="batchDel">批量删除</button>
        <button type="button" class="layui-btn" id="TJ" lay-event="writeUser">
            <i class="layui-icon">&#xe608;</i> 添加角色
        </button>

    </script>
    <script>
        layui.use(['laydate', 'laypage', 'layer', 'table'], function () {
            var laydate = layui.laydate //日期
                , laypage = layui.laypage //分页
                , layer = layui.layer //弹层
                , table = layui.table //表
            //执行一个 table 实例
            table.render({
                elem: '#demo'
                , height: 420
                , url: '/System/Search' //数据接口
                , title: '角色表'
                , page: true //开启分页
                , toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , totalRow: true //开启合计行
                 , id: 'testReload'
                , cols: [[ //表头
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'RoleName', title: '角色名', width: 80 }
                    , { fixed: 'right', width: 165, align: 'center', toolbar: '#barDemo' }
                ]]
            });
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        console.log(obj.data.Id)
                        $.post("/System/Role_Del", { id: obj.data.Id }, function (res) {
                            if (res.success) {
                                active["reload"].call(null);
                                layer.msg("成功", obj.othis);
                            }
                            else {
                                layer.msg("失败,请重试", obj.othis);

                            }

                        }, "json");
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: "编角色信息",
                        area: ["30%", "65%"],
                        content: '/System/Role_Edit?Id=' + obj.data.Id,
                        end: function () {
                            tableIns.reload({
                                where: { //设定异步数据接口的额外参数，任意设
                                    Name: $("#Name").val()
                                    //…
                                }
                            });
                        }
                    });
                }
                else if (obj.event === 'Power') {
                    layer.open({
                        type: 2,
                        title: "信息",
                        area: ["30%", "65%"],
                        content: '/System/UpdateModulees?Id=' + obj.data.Id,
                        end: function () {
                            tableIns.reload({
                                where: { //设定异步数据接口的额外参数，任意设
                                    Name: $("#Name").val()
                                    //…
                                }
                            });
                        }
                    });
                }
            });
            table.on('toolbar(test)', function (obj) {
                if (obj.event === 'writeUser') {
                    layer.open({
                        type: 2,
                        title: "添加角色",
                        area: ['50%', '50%'],
                        content: '/System/Role_Add',
                        end: function () {
                            tableIns.reload({
                                where: { //设定异步数据接口的额外参数，任意设
                                    Name: $("#Name").val()
                                    //…
                                }
                            });
                        }
                    });
                }
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'batchDel':
                        var data = checkStatus.data;
                        var ids = [];
                        for (var i = 0; i < data.length; i++) {
                            let obj = data[i];
                            ids.push(obj.Id);
                        }
                        //layer.alert(ids.join(','));
                        if (ids.length == 0) {
                            layer.msg("您必须勾选要删除的角色数据");
                            return;
                        }
                        var idsx = ids.join(',');
                        console.log("需要删除的数据为：" + idsx);
                        $.post("/System/Pubdel", { ids: idsx }, function (res) {
                            if (res.success) {
                                active["reload"].call(null);
                                layer.msg("删除成功");
                            }
                            else
                                layer.alert(res.message);
                        }, "json");
                        break;
                };
            });
            active = {
                reload: function () {

                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                        }
                    }, 'data');
                }
            };
            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
</body>
</html >
